/*
 * sun_Net_ActorState
 * 仿世界ol-角色状态界面
 * 
 */



//改Gauge精灵
Sprite_Gauge.prototype.bitmapWidth = function () {
    return 256;
}

Sprite_Gauge.prototype.drawValue = function () {
    const currentValue = this.currentValue();
    const maxValue = this.currentMaxValue();
    const width = this.bitmapWidth();
    const height = this.bitmapHeight();
    const text = `${currentValue}/${maxValue}`
    this.setupValueFont();
    this.bitmap.drawText(text, 0, 0, width, height, "right");
}

Sprite_Gauge.prototype.currentValue = function () {
    if (this._battler) {
        switch (this._statusType) {
            case "hp":
                return this._battler.hp;
            case "mp":
                return this._battler.mp;
            case "tp":
                return this._battler.tp;
            case "time":
                return this._battler.tpbChargeTime();
            case "exp":
                return this._battler.currentExp() - this._battler.currentLevelExp();
        }
    }
    return NaN;
}

Sprite_Gauge.prototype.currentMaxValue = function () {
    if (this._battler) {
        switch (this._statusType) {
            case "hp":
                return this._battler.mhp;
            case "mp":
                return this._battler.mmp;
            case "tp":
                return this._battler.maxTp();
            case "time":
                return 1;
            case "exp":
                return this._battler.nextRequiredExp();
        }
    }
    return NaN;
}

Sprite_Gauge.prototype.label = function () {
    switch (this._statusType) {
        case "hp":
            return TextManager.hpA;
        case "mp":
            return TextManager.mpA;
        case "tp":
            return TextManager.tpA;
        case "exp":
            return "经验值";
        default:
            return "";
    }
}

Sprite_Gauge.prototype.gaugeColor1 = function () {
    switch (this._statusType) {
        case "hp":
            return ColorManager.hpGaugeColor1();
        case "mp":
            return ColorManager.mpGaugeColor1();
        case "tp":
            return ColorManager.tpGaugeColor1();
        case "time":
            return ColorManager.ctGaugeColor1();
        case "exp":
            return "#EEEE00";
        default:
            return ColorManager.normalColor();
    }
}

Sprite_Gauge.prototype.gaugeColor2 = function () {
    switch (this._statusType) {
        case "hp":
            return ColorManager.hpGaugeColor2();
        case "mp":
            return ColorManager.mpGaugeColor2();
        case "tp":
            return ColorManager.tpGaugeColor2();
        case "time":
            return ColorManager.ctGaugeColor2();
        case "exp":
            return "#EEE685";
        default:
            return ColorManager.normalColor();
    }
}

Sprite_Gauge.prototype.valueColor = function () {
    switch (this._statusType) {
        case "hp":
            return ColorManager.hpColor(this._battler);
        case "mp":
            return ColorManager.mpColor(this._battler);
        case "tp":
            return ColorManager.tpColor(this._battler);
        case "exp":
            return "#FFFFFF";
        default:
            return ColorManager.normalColor();
    }
}

//角色状态场景
class Scene_ActorState extends Scene_Base {
    initialize() {
        super.initialize();
        NetMap.needPlacePlayer = true;
    }

    create() {
        super.create();
        // this.createBackground();
        this.updateActor();
        this.createWindowLayer();
        this.createAllWindow();
        this.createButtonGroup();
        this.createBackButton();
    }

    updateActor() {
        this._actor = NetManager.netActor();
    }

    createAllWindow() {
        this.createBaseWindow();
        this.createParamWindow();
        this.createSkillWindow();
        this.createTipWindow();
    }

    createBaseWindow() {
        const rect = this.baseWindowRect();
        this._baseWindow = new Window_ActorBase(rect);
        this.addWindow(this._baseWindow);
    }

    baseWindowRect() {
        const wx = 0;
        const ww = Graphics.boxWidth;
        const wh = 1000;
        const wy = (Graphics.boxHeight - wh) / 2;
        return new Rectangle(wx, wy, ww, wh);
    }

    createParamWindow() {
        const rect = this.baseWindowRect();
        this._paramWindow = new Window_ActorParam(rect);
        this.addWindow(this._paramWindow);
        this._paramWindow.hide();
    }

    createSkillWindow() {
        const rect = this.baseWindowRect();
        this._skillWindow = new Window_ActorSkill(rect);
        this.addWindow(this._skillWindow);
        this._skillWindow.hide();
    }

    createTipWindow() {
        const rect = this.tipWindowRect();
        this._tipWindow = new Window_Tip(rect);
        this.addWindow(this._tipWindow);
    }

    tipWindowRect() {
        const ww = Graphics.boxWidth;
        const wh = 60;
        const wx = 0;
        const wy = 150;
        return new Rectangle(wx, wy, ww, wh);
    }

    createButtonGroup() {
        let x = 160;
        const y = this.baseWindowRect().y - 50;
        const text = ["基本", "属性", "技能"];
        this._buttonGroup = [];
        for (let i = 0; i < 3; i++) {
            const bitmap = this.createButtonGroupBitmap(text[i]);
            const button = new Sprite_NetButton(x, y, bitmap, this.onButtonChick.bind(this, i));
            this._buttonGroup.push(button);
            this.addChild(button);
            x += 150;
        }
        this.onButtonChick(0);
    }

    onButtonChick(index) {
        switch (index) {
            case 0:
                this._baseWindow.show();
                this._paramWindow.hide();
                this._skillWindow.hide();
                break;
            case 1:
                this._baseWindow.hide();
                this._paramWindow.show();
                this._skillWindow.hide();
                break;
            case 2:
                this._baseWindow.hide();
                this._paramWindow.hide();
                this._skillWindow.show();
                break;
            default:
                break;
        }
        this.buttonGroupRevert();
        const button = this._buttonGroup[index];
        button._state = "select";
        button.move(button.x, button.y + 20);
    }

    buttonGroupRevert() {
        let x = 160;
        const y = this.baseWindowRect().y - 50;
        const text = ["基本", "属性", "技能"];
        for (let i = 0; i < 3; i++) {
            const button = this._buttonGroup[i];
            button._state = null;
            button.move(x, y);
            x += 150;
        }
    }

    createButtonGroupBitmap(text, select = false) {
        const w = 100;
        const h = 100;
        const c = select ? ColorManager.textColor(8) : ColorManager.textColor(9);
        const bitmap = new Bitmap(w, h);
        bitmap.fillRoundRect(0, 0, w, h / 2, c);
        bitmap.fillRoundRect(0, h / 2, w, h / 2, ColorManager.textColor(8));
        bitmap.fontSize = 30;
        bitmap.drawText(text, 0, 0, w, h / 2, "center");
        bitmap.drawText(text, 0, h / 2, w, h / 2, "center");
        return bitmap;
    }

    createBackButton() {
        const x = Graphics.boxWidth - 121;
        const y = 25;
        const button = new Sprite_Button("cancel");
        button.setClickHandler(this.popScene.bind(this));
        this._backButton = button;
        this.addChild(button);
        button.move(x, y);
    }
}

class Window_ActorBase extends Window_Base {
    initialize(rect) {
        super.initialize(rect);
        this.iniPoints();
        this.drawBackground();
        this.drawBaseInfo();
        this.drawPoints();
        this.createButtons();
        this.refresh();
    }

    iniPoints() {
        const actor = NetManager.netActor();
        this._tempPoints = actor._points;
        this._tempPointsBase = [0, 0, 0, 0, 0];
    }

    drawBackground() {
        const x = 20;
        const y = 33;
        const w = Graphics.boxWidth / 3 + 30;
        const h = 50;
        const spacing = 50;
        const c = "#EAEAEA32"
        this.contents.fillRoundRect(x, y + spacing, w, h, c, 0);
        this.contents.fillRoundRect(x, y + spacing * 3, w, h, c, 0);
        this.contents.fillRoundRect(x, y + spacing * 5, w, h, c, 0);
    }

    drawBaseInfo() {
        this.drawInfo();
        this.drawCharacter();
        this.placeGauge();
    }

    drawInfo() {
        const x = 25;
        const y = 40;
        const w = Math.floor(Graphics.boxWidth / 4);
        const spacing = 50;
        const actor = NetManager.netActor();
        this.contents.fontSize = 30;
        this.drawText("名称:", x, y, w, "left");
        this.drawText("职业:", x, y + spacing, w, "left");
        this.drawText("等级:", x, y + spacing * 2, w, "left");
        this.drawText("ID:", x, y + spacing * 3, w, "left");
        this.drawText("称号:", x, y + spacing * 4, w, "left");
        this.drawText("公会:", x, y + spacing * 5, w, "left");
        this.drawText("职位:", x, y + spacing * 6, w, "left");
        this.changeTextColor("#CD6839");
        this.drawText(actor.name(), x + 100, y, w, "center");
        this.drawText(actor.className(), x + 100, y + spacing, w, "center");
        this.drawText(`${actor.level}级`, x + 100, y + spacing * 2, w, "center");
        this.drawText(`暂无`, x + 100, y + spacing * 3, w, "center");
        this.drawText(`暂无`, x + 100, y + spacing * 4, w, "center");
        this.drawText(`暂无`, x + 100, y + spacing * 5, w, "center");
        this.drawText(`暂无`, x + 100, y + spacing * 6, w, "center");
        this.resetTextColor();
        this.resetFontSettings();
    }

    drawCharacter() {
        const actor = NetManager.netActor();
        const characterName = actor.characterName();
        const characterIndex = actor.characterIndex();
        const x = Graphics.boxWidth / 4 * 3;
        const y = 250;
        super.drawCharacter(characterName, characterIndex, x, y);
    }

    placeGauge() {
        const x = Graphics.boxWidth / 2 + 25;
        const y = 250;
        const actor = NetManager.netActor();
        const hpSprite = new Sprite_Gauge();
        const mpSprite = new Sprite_Gauge();
        const expSprite = new Sprite_Gauge();
        hpSprite.setup(actor, "hp");
        mpSprite.setup(actor, "mp");
        expSprite.setup(actor, "exp");
        hpSprite.move(x, y);
        mpSprite.move(x, y + 40);
        expSprite.move(x, y + 80);
        hpSprite.show();
        mpSprite.show();
        expSprite.show();
        this._hpSprite = hpSprite;
        this._mpSprite = mpSprite;
        this._expSprite = expSprite;
        this.addInnerChild(hpSprite);
        this.addInnerChild(mpSprite);
        this.addInnerChild(expSprite);
    }

    drawPoints() {
        this.drawPointsBottom();
        this.createPointsContentSprite();
    }

    drawPointsBottom() {
        const x = 20;
        const y = 425;
        const w = Graphics.boxWidth - 60;
        const h = 400;
        const spacing = 60;
        const c = "#000000";
        const c2 = "#EAEAEA32";
        this.contents.fontSize = 40;
        this.changeTextColor("#CD6839");
        this.contents.fillRoundRect(x, y - 25, w, 50, c, 0);
        this.drawText(`角色属性`, x, y - 20, w, "center");
        this.contents.fillRoundRect(x, y + 30, w, h, c2, 0);
        this.contents.fontSize = 30;
        this.drawText(`可分配属性:`, x + 30, y + spacing, w);
        this.drawText(`力量:`, x + 30, y + spacing * 2, w);
        this.drawText(`体质:`, x + 30, y + spacing * 3, w);
        this.drawText(`敏捷:`, x + 30, y + spacing * 4, w);
        this.drawText(`智力:`, x + 30, y + spacing * 5, w);
        this.drawText(`精神:`, x + 30, y + spacing * 6, w);
        this.resetTextColor();
        this.resetFontSettings();
    }

    createPointsContentSprite() {
        const x = 30;
        const y = 465;
        const w = Graphics.boxWidth - 60;
        const h = 400;
        const sprite = new Sprite(new Bitmap(w, h));
        sprite.move(x, y);
        // sprite.bitmap.fillRoundRect(0, 0, w, h, "#00FF00", 0);
        this.addChild(sprite);
        this._pointsContent = sprite;
    }

    createButtons() {
        this.createPointsButton();
        this.createOkButton();
    }

    createPointsButton() {
        const x = Graphics.boxWidth - 225;
        let y = 550;
        const spacing = 60;
        this._addButtons = [];
        this._reduceButtons = [];
        for (let i = 0; i < 5; i++) {
            const addButton = new Sprite_Button("up");
            const reduceButton = new Sprite_Button("down");
            addButton.setClickHandler(this.addParam.bind(this, i));
            reduceButton.setClickHandler(this.reduceParam.bind(this, i));
            this._addButtons.push(addButton);
            this._reduceButtons.push(reduceButton);
            this.addChild(addButton);
            this.addChild(reduceButton);
            addButton.move(x, y);
            reduceButton.move(x + 120, y);
            y += spacing
        }
    }

    createOkButton() {
        const x = Graphics.boxWidth - 225;
        const y = 485;
        const okButton = new Sprite_Button("ok");
        okButton.setClickHandler(this.pointsOk.bind(this));
        this._okButtons = okButton;
        this.addChild(okButton);
        okButton.move(x, y);
        okButton.hide();
    }

    addParam(index) {
        if (this._tempPoints <= 0) return;
        this._tempPointsBase[index]++;
        this._tempPoints--;
        this.refresh();
    }

    reduceParam(index) {
        if (this._tempPointsBase[index] <= 0) return;
        this._tempPointsBase[index]--;
        this._tempPoints++;
        this.refresh();
    }

    pointsOk() {
        const actor = NetManager.netActor();
        NetManager.dataServer.emit('update points', this._tempPointsBase);
        this._okButtons.hide();
    }

    refresh() {
        const actor = NetManager.netActor();
        if (this._tempPoints != actor._points) {
            this._okButtons.show();
        } else {
            this._okButtons.hide();
        }
        this._pointsContent.bitmap.clear();
        this.drawActorPoints();
        this.drawActorPointsBase();
        this.drawTempPoints();
    }

    drawActorPoints() {
        const x = 240;
        const y = 35;
        const w = 100;
        const h = 30;
        const bitmap = this._pointsContent.bitmap;
        bitmap.fontSize = 30;
        bitmap.textColor = "#008B00";
        bitmap.drawText(this._tempPoints, x, y, w, h);
    }

    drawActorPointsBase() {
        const x = 160;
        let y = 95;
        const w = 100;
        const h = 30;
        const spacing = 60;
        const bitmap = this._pointsContent.bitmap;
        const actor = NetManager.netActor();
        bitmap.textColor = "#FFFFFF";
        for (let i = 0; i < 5; i++) {
            let value;
            if (actor._pointsPlus[i] > 0) {
                value = `${actor._pointsBase[i]}(+${actor._pointsPlus[i]})`;
            } else {
                value = `${actor._pointsBase[i]}`;
            }
            bitmap.drawText(value, x, y, w, h);
            y += spacing;
        }
        this.drawTempPoints();
    }

    drawTempPoints() {
        const x = 320;
        let y = 95;
        const w = 100;
        const h = 30;
        const spacing = 60;
        const bitmap = this._pointsContent.bitmap;
        const actor = NetManager.netActor();
        for (let i = 0; i < 5; i++) {
            let value = this._tempPointsBase[i] + actor._pointsBase[i];
            if (this._tempPointsBase[i] > 0) {
                bitmap.textColor = "#00FF00";
            } else {
                bitmap.textColor = "#FFFFFF";
            }
            bitmap.drawText(value, x, y, w, h);
            y += spacing;
        }
    }
}

class Window_ActorParam extends Window_Selectable {
    initialize(rect) {
        super.initialize(rect);
        this._paramList = ["agi", "atn", "atk", "mat", "def", "mdf", "eva", "hit", "mht", "cri", "crd", "sre", "drn", "bka", "mpn", "ist", "trb"];
        this._paramText = { "agi": "出手速度", "atn": "攻击次数", "atk": "物理攻击", "mat": "魔法攻击", "def": "物理防御", "mdf": "魔法防御", "eva": "闪避", "hit": "物理命中", "mht": "魔法命中", "cri": "暴击", "crd": "暴击伤害", "sre": "状态抵抗", "drn": "伤害减免", "bka": "破甲", "mpn": "魔法穿透", "ist": "洞察", "trb": "治疗加成" };
        this.refresh();
    }

    maxItems() {
        return this._paramList ? this._paramList.length : 1;
    }

    drawItem(index) {
        const param = this._paramList[index];
        const actor = NetManager.netActor();
        if (param) {
            const rect = this.itemLineRect(index);
            this.drawText(this._paramText[param], rect.x + 20, rect.y, 150);
            this.drawText(actor[param], rect.x + 300, rect.y, 150);
        }
    }

    itemHeight() {
        return 80;
    }

    refresh() {
        this.contents.fontSize = 35;
        super.refresh();
    }
}

class Window_ActorSkill extends Window_Selectable {
    initialize(rect) {
        super.initialize(rect);
    }


}